package com.transaction.demo.test;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class TwoFind {

    public static Random random = new Random();
    public static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int[] arr = new int[10];

        initArray(arr);

        System.out.println(Arrays.toString(arr));

        paixuSort(arr);

        //bubbleSort(arr);

        //selectSort(arr);

        //Arrays.sort(arr);

       // System.out.println(Arrays.toString(arr));

        int num = scanner.nextInt();

         //TODO温故3
        int index=0;
        int left=0;
        int right=arr.length;

        while(left<right){
            int mid =(left+right)/2;
            if(num>arr[mid]){
                left=mid;
            }else if(num<arr[mid]){
                right=mid;
            }else {
                index=mid;
                break;
            }

        }

    // int index =searchNum(arr,num);

      //  int index = binarySearch(arr, num);

        System.out.println(index);


    }

    private static int searchNum(int[] arr,int num) {

        int left=0;
        int right=arr.length;
        //温故3
        while(left<right){
            int mid=(left+right)/2;
            if(num>arr[mid]){
                left=mid+1;
            }else if(num<arr[mid]){
                right=mid-1;
            }else{
               return mid;
            }

        }

        return 0;
    }

    private static void paixuSort(int[] arr) {
        for(int i=0;i<arr.length;i++){
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]>arr[j]){
                     int temp= arr[i];
                     arr[i] =arr[j];
                     arr[j] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));

    }

    private static int binarySearch(int[] arr, int num) {

        int left = 0, right = arr.length - 1;

        while (left <= right) {
            int mid = (left + right) / 2;
            if (num > arr[mid])
                right = mid - 1;

            else if (num < arr[mid])
                left = mid + 1;
            else
                return mid;
        }
        return -1;
    }

    private static void selectSort(int[] arr) {

        for (int i = 0; i < arr.length - 1; i++) {
            int maxIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] > arr[maxIndex])
                    maxIndex = j;
            }
            if (i != maxIndex) {
                int t = arr[i] ^ arr[maxIndex];
                arr[i] ^= t;
                arr[maxIndex] ^= t;
            }
        }
        System.out.println(Arrays.toString(arr));
    }

    private static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j + 1]) {
                    int t = arr[j] ^ arr[j + 1];
                    arr[j] ^= t;
                    arr[j + 1] ^= t;
                }

            }
        }

        System.out.println(Arrays.toString(arr));
    }

    private static void initArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
    }
}

